package com.crazymakercircle.entity;

import com.crazymakercircle.constants.ServerConstants;
import com.crazymakercircle.util.JsonUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

import static com.crazymakercircle.constants.ServerConstants.SDF;

/**
 * 工作节点的POJO类
 * create by 尼恩 @ 疯狂创客圈
 **/
@Data
public class TaskData implements Serializable
{
    private static final long serialVersionUID = -499010884211304846L;


    //任务名称

    private String json = "un-known";
    private String runId = "un-known";
    private String uploadTime = "un-known";
    private String runPath;


    public TaskData()
    {
        this.uploadTime = SDF.format(new Date());
    }


    @Override
    public String toString()
    {
        return "TaskData{" +
                "uploadTime='" + uploadTime + '\'' +
                '}';
    }

    public String toJson()
    {
        return JsonUtil.pojoToJson(this);
    }

    public static TaskData fromJson(String json)
    {
        return JsonUtil.jsonToPojo(json, TaskData.class);
    }

    public static TaskData fromJson(byte[] jsonBytes)
    {
        return JsonUtil.jsonBytes2Object(jsonBytes, TaskData.class);
    }


    public static String getRunIdByPath(String path)
    {
        String sid = null;
        if (null == path)
        {
            throw new RuntimeException("节点路径有误");
        }
        int index = path.lastIndexOf(ServerConstants.PATH_TASK_INST);
        if (index >= 0)
        {
            index += ServerConstants.PATH_TASK_INST.length();
            sid = index <= path.length() ? path.substring(index) : null;
        }

        if (null == sid)
        {
            throw new RuntimeException("节点ID获取失败 path:" + path);
        }

        return sid;

    }


}